home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dutil / loadfile.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  88 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6. /*
  7. **    $Id: loadfile.c,v 30.326 1995/12/24 06:13:05 dice Exp dice $
  8. **
  9. **    Load a binary file into memory.  Report the address and hole
  10. **    until CTRL-C is pressed.  Useful for looking at binary files
  11. **    with a debugger.
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <exec/memory.h>
  17. #include <dos/dos.h>
  18. #include <clib/exec_protos.h>
  19. #ifdef AMIGA
  20. #include <lib/version.h>
  21. #else
  22. #include <include/lib/version.h>
  23. #endif
  24.  
  25. #define GUARD    100    /* Padding on either side of allocation */
  26. #define FILL    0xaa    /* Character used to fill guard area */
  27.  
  28. #ifdef _DCC
  29. IDENT("loadfile",".2");
  30. DCOPYRIGHT;
  31. #endif
  32.  
  33. void    onbreak((*fptr)());
  34. void    exit(int);
  35.  
  36. disable_break()
  37. {
  38.     return(0);
  39. }
  40.  
  41. main(int argc,char *argv[])
  42. {
  43. FILE *    fp;
  44. long    fl;
  45. char *    memory;
  46. char *    filptr;
  47. int    err;
  48.  
  49.     onbreak(disable_break);
  50.     SetSignal(SIGBREAKF_CTRL_C,0);    /* Clear CTRL-C signal */
  51.  
  52.     if (argc != 2 || ( argc == 2  &&  *argv[1]=='?' ) )
  53.     {
  54.         printf("loadfile <filename>\n");
  55.         printf(";Load binary file into memory, wait for CTRL-C\n");
  56.         exit(5);
  57.     }
  58.  
  59.     if( fp=fopen(argv[1],"r") )
  60.     {
  61.         fseek(fp,0,2);  /* Seek to end of file */
  62.         fl=ftell(fp);
  63.         fseek(fp,0,0);  /* Seek to start of file */
  64.         if (memory=AllocMem(fl+GUARD+GUARD,0) )
  65.         {
  66.         for(filptr=memory; filptr<(memory+fl+GUARD+GUARD); filptr++)
  67.             *filptr=FILL;
  68.         err=fread((memory+GUARD),1,fl,fp);
  69.         if(err)
  70.         {
  71.             printf("Loaded %ld ($%lx) bytes at location $%lx\n",
  72.                 fl,fl,memory+GUARD);
  73.             fseek(fp,0,0);  /* Seek to start of file */
  74.             printf("Press CTRL-C to exit\n");
  75.             Wait(SIGBREAKF_CTRL_C);
  76.         }
  77.         else
  78.             printf("Error reading file %s, error %d\n",argv[1],err);
  79.         FreeMem(memory,fl+GUARD+GUARD);
  80.         }
  81.         else
  82.         printf("Error: out of memory\n");
  83.         fclose( fp );
  84.     }
  85.     else
  86.         printf("Could not open file %s\n",argv[1]);
  87. }
  88.